package praca.framework;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.List;

import praca.impl.Wynik;
import praca.interfejsy.FileIO;
import praca.interfejsy.Game;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class FileIOGL implements FileIO {

	private Game game;
	InputStream in = null;
	AssetManager manager;
	private File appDirectory;

	public FileIOGL(Game gra) {
		game = gra;
		manager = game.getAssetManager();
		appDirectory = ((Context) game).getFilesDir();
	}

	public boolean saveHighScores(List<Wynik> scores) {
		try {
			File file = new File(appDirectory, "scores.lis");
			FileOutputStream fileOut = new FileOutputStream(file);
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(scores);
			out.close();
			fileOut.close();
		} catch (IOException i) {
			i.printStackTrace();
			Log.d("zapis","Blad: "+i);
			return false;
		}
		return true;
	}

	public List<Wynik> loadHighScores() {
		File file = new File(appDirectory, "scores.lis");

		List<Wynik> wynik = null;
	      try
	      {
	         FileInputStream fileIn = new FileInputStream(file);
	         ObjectInputStream in = new ObjectInputStream(fileIn);
	         wynik = (List<Wynik>) in.readObject();
	         in.close();
	         fileIn.close();
	      }catch(IOException i)
	      {
	         i.printStackTrace();
	         return null;
	      }catch(ClassNotFoundException c)
	      {
	         System.out.println("Employee class not found");
	         c.printStackTrace();
	         return null;
	      }
		
		return wynik;
	}

	public boolean saveSettings(String settings) {
		File file = new File(appDirectory, "settings.txt");
		PrintWriter out = null;

		try {
			out = new PrintWriter(file);
			out.println(settings);
			out.flush();
			Log.d("tekst", "Zapisano");
		} catch (FileNotFoundException e) {
			Log.d("tekst", "Exception");
			e.printStackTrace();
			return false;
		}
		out.close();
		return true;
	}

	public StringBuilder readSettings() {
		StringBuilder sb = new StringBuilder();
		BufferedReader br = null;
		File file = new File(appDirectory, "settings.txt");
		try {
			br = new BufferedReader(new FileReader(file));
			String line = br.readLine();

			while (line != null) {
				sb.append(line);
				sb.append('\n');
				line = br.readLine();
			}
			String everything = sb.toString();
			br.close();
		} catch (Exception e) {

		} finally {

		}

		return sb;
	}

	@Override
	public Bitmap loadBitmap(String name) throws IOException {
		Bitmap bitmap;

		in = manager.open(name);
		bitmap = BitmapFactory.decodeStream(in);

		in.close();

		return bitmap;
	}

}
